// @ts-nocheck 
import {defineStore} from 'pinia';

import{reqGetCategory1ListData,reqGetCategory2ListData,reqGetCategory3ListData} from '@/api/product/category'

// 创建模块对象
export const useCategoryListStore = defineStore('categoryList',{
    state:()=>{
        return {
            // 以下三个属性用于收集用户真正选择的分类的id
            category1Id: undefined,
            category2Id: undefined,
            category3Id: undefined,

            // 以下三个数组用于存储服务器响应回来的分类数据
            category1List: [],
            category2List: [],
            category3List: []
        }
    },
    actions:{
        // 1.获取一级分类数据
        async getCategory1ListData(){
            const result = await reqGetCategory1ListData()
            this.category1List = result
        },
        // 2. 获取二级分类数据
        async getCategory2ListData(category1Id: number) {
            this.category1Id = category1Id
            // 如果1级导航变了，二三级需要充值 即须将id设为undefined
            this.category2Id = undefined
            this.category3Id = undefined
            this.category3List = [] // 三级列表清空
            this.category2List = await reqGetCategory2ListData(category1Id)
        },
        // 3. 获取三级分类数据
        async getCategory3ListData(category2Id: number) {
            this.category2Id = category2Id
            this.category3List = await reqGetCategory3ListData(category2Id)

             // 修改二级分类 则三级也需要重置
             this.category3Id = undefined
        },
        // 4. 设置三级分类ID
        setCategory3Id(category3Id:number) {
            this.category3Id = category3Id
        }
    }
})